package com.zmn.brs.admin.controller.rule.objective;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import com.zmn.brs.model.entity.event.Event;
import com.zmn.brs.model.query.rule.ObjectiveRuleCustom;
import com.zmn.brs.model.query.rule.ObjectiveRuleCustomQuery;
import com.zmn.brs.services.interfaces.event.EventService;
import com.zmn.okr.common.constant.ObjectiveMasterTypeConst;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;


/**
 * 描述： 绩效目标规则
 * @author chongw
 * @since 2021-05-28 17:05:29
 */
@Controller
@RequestMapping("/brs/objective/rule/")
public class ObjectiveRuleListController extends ObjectiveRuleBaseController {

    @Resource
    private EventService eventService;

    /**
     * 列表
     * @author chongw
     * @param query
     * @since 2021-05-28 17:05:29
     */
    @RequestMapping("list")
    public ModelAndView list(@ModelAttribute ObjectiveRuleCustomQuery query){
        Event event = eventService.findById(query.getEventId());
        query.setEventCode(event.getEventCode());
        //查询部门绩效规则配置
        query.setObjMasterType(ObjectiveMasterTypeConst.MASTERTYPE_ALL_DEPT);
        List<ObjectiveRuleCustom> deptItems = Optional.ofNullable(objectiveRuleService.listObjectiveRuleByQuery(query)).orElse(new ArrayList<>());

        query.setObjMasterType(ObjectiveMasterTypeConst.MASTERTYPE_PERSON);
        //查询责任人绩效规则配置
        List<ObjectiveRuleCustom> personItems = objectiveRuleService.listPageObjectiveRuleByQuery(query);
        ModelAndView mav = new ModelAndView("objective/rule/list");
        mav.addObject("items", personItems);
        mav.addObject("deptItems", deptItems);
        mav.addObject("vo", query);
        mav.addObject("flag",2);
        mav.addObject("eventId", event.getEventId());
        mav.addObject("sceneId", event.getSceneId());
        mav.addObject("data", event);
        return mav;
    }

    /**
     * 规则配置
     * @author chongw
     * @param query
     * @since 2021-06-01 17:05:29
     */
    @RequestMapping("ruleInfo")
    public ModelAndView ruleInfo(@ModelAttribute ObjectiveRuleCustomQuery query){
        //查询部门绩效规则配置
        List<ObjectiveRuleCustom> ruleCustomList = Optional.ofNullable(objectiveRuleService.listObjectiveRuleByQuery(query)).orElse(new ArrayList<>());
        ModelAndView mav = new ModelAndView("objective/rule/ruleInfo");
        if(!ruleCustomList.isEmpty()){
            mav.addObject("rule", ruleCustomList.get(0));
        }
        return mav;
    }

}
